package com.example.term;



import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;

public class MainActivity extends AppCompatActivity {
    Button btnlogin, btnregister,btnquit;
    CheckBox btncheckbox;
    EditText txtpassd, txtlogin;

    // 声明一个SharedPreferences用于保存数据
    private SharedPreferences spSettings = null;
    private static final String PREFS_NAME = "NamePwd";
    MyHelper myhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        myhelper=new MyHelper(this);

        setContentView(R.layout.activity_main);
        btnlogin = (Button) findViewById(R.id.btlogin);
        btnregister = (Button) findViewById(R.id.btnregister);
        btncheckbox = (CheckBox) findViewById(R.id.checkBox);
        btnquit = (Button)findViewById(R.id.btn_quit);

        txtpassd = (EditText) findViewById(R.id.passwd);
        txtlogin = (EditText) findViewById(R.id.login);
        Intent intent = new Intent();
//        String resd = intent.getStringExtra("resd");
        Intent intentd = getIntent();
        getData();

        //登录按钮
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usr_login();
            }
        });
        btnquit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                System.exit(0);
            }
        });
        // 用户注册
        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usr_register();
            }
        });

    }
    public void usr_register(){
        String login;
        String passwd;
        SQLiteDatabase db;
        ContentValues values;
        String[] columns = {"name"};
        login = txtlogin.getText().toString();
        passwd = txtpassd.getText().toString();
        if(login.equals("")||login.equals(null)){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
        }
        if(passwd.equals("")||passwd.equals(null)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
        }
        else {
            db = myhelper.getWritableDatabase();
            Cursor cursor = db.query("information", columns, "name like ?", new String[]{login},
                    null, null, null);
//        Cursor cursor = db.query("information",null, null,null,
//                                null,null,null);
            if (cursor.getCount() > 0) { //有用户
                Toast.makeText(getApplicationContext(), "用户名已经存在,无需注册", Toast.LENGTH_SHORT).show();

            }

            else {
                db = myhelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name", login);
                values.put("passwd", passwd);
                db.insert("information", null, values);
                Toast.makeText(this, "已注册", Toast.LENGTH_SHORT).show();
                db.close();
            }
        }

    }


    public void usr_login() {
        String login;
        String passwd;
        SQLiteDatabase db;
        ContentValues values;
        String[] columns = {"name"};
        login = txtlogin.getText().toString();
        passwd = txtpassd.getText().toString();
        db = myhelper.getReadableDatabase();
        Cursor cursor = db.query("information", columns, "name like ?", new String[]{login},
                null, null, null);
//        Cursor cursor = db.query("information",null, null,null,
//                                null,null,null);
        if (cursor.getCount() == 0) { //没有用户
            Toast.makeText(getApplicationContext(), "用户名不存在,请先注册", Toast.LENGTH_SHORT).show();

        } else {    // 有用户
            Cursor cursord = db.query("information", new String[]{"passwd"}, "passwd like ?", new String[]{passwd},
                    null, null, null);
            if (cursord.getCount() > 0) {
                if (btncheckbox.isChecked()) {
                    spSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    // 得到Editor对象
                    SharedPreferences.Editor edit = spSettings.edit();
                    // 记录保存标记
                    edit.putBoolean("isKeep", true);
                    Toast.makeText(getApplicationContext(), "账号密码将被保存！",
                            Toast.LENGTH_SHORT).show();
                    // 记录用户名
                    edit.putString("username", txtlogin.getText().toString());
                    // 记录密码
                    edit.putString("password", txtpassd.getText().toString());
                    edit.commit(); // 提交
                } else {
                    spSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    // 得到Editor对象
                    SharedPreferences.Editor edit = spSettings.edit();
                    // 记录保存标记
                    edit.putBoolean("isKeep", false);
                    // 记录用户名
                    edit.putString("username", "");
                    // 记录密码
                    edit.putString("password", "");
                    edit.commit(); // 提交
                    txtlogin.setText("");
                    txtpassd.setText("");
                }
                // 跳转到首页
                Intent intent = new Intent(MainActivity.this, login.class);
                intent.putExtra("username", txtlogin.getText().toString());
                intent.putExtra("password", txtpassd.getText().toString());
                //                    startActivityForResult(intent, 1);
                startActivityForResult(intent, 1);  //15时页面B的表示
                //                    startActivity(intent);
            }
            else {
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
            }
            cursor.close();
            db.close();
        }
    }
    private void getData() {
        String content = null;
        // 得到sharedpreferences对象
        spSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        btncheckbox.setChecked(spSettings.getBoolean("isKeep", Boolean.parseBoolean("")));
        FileInputStream fis;


//        txt_resd.setText();
        // 判断是否之前存储过用户名密码
        if (btncheckbox.isChecked()) {
            // 如果之前存储过，则显示在相应文本框内
            txtlogin.setText(spSettings.getString("username", ""));
            txtpassd.setText(spSettings.getString("password", ""));
        } else {
            // 否则显示空
            txtlogin.setText("");
            txtpassd.setText("");
        }
    }
    public void onActivityResult(int requestCode ,int resaultCode ,Intent data){
        super.onActivityResult(requestCode,resaultCode,data);
        if (requestCode==1 && resaultCode==2){
            String resault = data.getStringExtra("result");
            

        }
    }
    class MyHelper extends SQLiteOpenHelper {
        public MyHelper(Context context){
            super(context,"itcast.db",null,1);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("CREATE TABLE information (_id INTEGER PRIMARY KEY " +
                    "AUTOINCREMENT,name char(11),passwd char(10))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}